new Vue({
    el: '#app',
    data: {
        defaultArea: window.localStorage.getItem('city_id'),
        area: window.localStorage.getItem('city_id'),
        keyword: document.getElementById("keyword").value,
        region: 0,
        city: [],
        cityArea: [],
        townData:[],
        station: [],
        stationData: [],
        cur: 0,
        curMetro: 0,
        metro: [],
        metro_id: 0,
        minPrice: '',
        maxPrice: '',
        year: 0,
        searchStatus: false,
        loading: false,
        proData: '',
        current: 1,
        pageSize: 10,
        total: 0,
        sort: 'default',
        send_loading: false
    },
    methods: {
        clearKeywords(){
            this.keyword = ''
            this.getData()
        },
        getData(){
            if (this.loading) return
            this.loading = true
            var that = this
            var formData = {
                keyword: that.keyword,
                area:that.area,
                min_price:that.minPrice,
                max_price:that.maxPrice,
                year:that.year,
                pagesize:that.pageSize,
                sort: that.sort,
                town: that.townData,
                station: that.stationData,
                metro_id: that.metro_id
            }
            axios.post(`/community/data?page=${this.current}`,formData).then(function (response) {
                if(response.data.code === 0){
                    that.loading = false
                    that.searchStatus = true
                    that.proData = response.data.data.data
                    that.total = response.data.data.total
                }else{
                    that.$Message.error('数据获取失败，请稍后再试')
                }
            }).catch(function (error) {
                that.loading = false
            });
        },
        handleChangeSize(val) {
            this.pageSize = val
            this.$nextTick(() => {
                this.getData()
            })
        },
        changeSort(obj){
            this.sort = obj
            this.getData()
        },
        getCityAndMetro(){
            var that = this
            var formData = {
                area:that.area
            }
            axios.post('/region/city',formData).then(function (res) {
                if(res.data.code === 0){
                    that.city = res.data.city
                    that.metro = res.data.metro
                }else{
                    that.$Message.error('数据获取失败，请稍后再试')
                }
            }).catch(function (error) {
                // that.loading = false
            });
        },
        changeRegion(id){
            var that = this
            that.cur = id
            var formData = {
                area_id:id
            }
            that.area = id
            that.townData = []
            that.$nextTick(function () {
                that.getData()
            })
            axios.post('/region/cityArea',formData).then(function (res) {
                if(res.data.code === 0){
                    that.cityArea = res.data.area
                }else{
                    that.$Message.error('数据获取失败，请稍后再试')
                }
            }).catch(function (error) {
                // that.loading = false
            });
        },
        changeMetro(id){
            var that = this
            that.curMetro = id
            var formData = {
                metro_id:id
            }
            that.metro_id = id
            that.stationData = []
            that.$nextTick(function () {
                that.getData()
            })
            axios.post('/region/metroStation',formData).then(function (res) {
                if(res.data.code === 0){
                    that.station = res.data.station
                }else{
                    that.$Message.error('数据获取失败，请稍后再试')
                }
            }).catch(function (error) {
                // that.loading = false
            });
        },
        changeTab(){
            this.area = window.localStorage.getItem('city_id')
            this.cityArea = []
            this.townData = []
            this.station = []
            this.stationData = []
            this.cur = 0
            this.curMetro = 0
            this.metro_id = 0
            this.$nextTick(function () {
                this.getData()
            })
        }
    },
    created(){
        this.getCityAndMetro()
    },
    mounted() {
        this.getData()
    }
})